import { _decorator, Camera, Component, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CameraFollow')
export class CameraFollow extends Component {
    @property(Node)
    target: Node = null!; // 主角节点

    private camera: Camera | null = null;

    start() {
        this.camera = this.node.getComponent(Camera);
        if (!this.camera || !this.target) {
            console.error("相机或目标未绑定！");
        }
    }

    lateUpdate(deltaTime: number) {
        if (this.target && this.camera) {
            // 保持Z轴不变，仅跟随X/Y轴
            const newPos = new Vec3(
                this.target.position.x,
                this.target.position.y,
                this.node.position.z
            );
            this.node.setPosition(newPos);
        }
    }
}